<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{
              item.pubdate | relativeTime
            }}</span>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="hToggleLike(item)"
        />
      </van-cell>
    </van-list>

    <!-- 评论列表 -->
    <!-- 发布评论 -->
    <div :class="commentShow ? 'art-cmt-container-1' : 'art-cmt-container-2'">
      <!-- 底部添加评论区域 - 1 -->
      <div class="add-cmt-box van-hairline--top" v-show="commentShow">
        <van-icon name="arrow-left" size="24px" @click="$router.back()" />
        <div class="ipt-cmt-div" @click="hShowCommentArea">发表评论</div>
        <div class="icon-box">
          <van-badge content="10" max="99">
            <van-icon name="comment-o" size="24px" />
          </van-badge>
          <van-icon name="star-o" size="24px" />
          <van-icon name="share-o" size="24px" />
        </div>
      </div>

      <!-- 底部添加评论区域 - 2 -->
      <div class="cmt-box van-hairline--top" v-show="!commentShow">
        <textarea
          ref="txt"
          @blur="hBlur"
          placeholder="友善评论、理性发言、阳光心灵"
          v-model.trim="commentText"
        ></textarea>
        <van-button type="default" @click="hAddComment">发布</van-button>
      </div>
    </div>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import { getComment, addComment, addCommentLike, deleteCommentLike } from '../../api/comment'
export default {
  name: 'ArticleComment',
  data () {
    return {
      commentText: '',
      commentShow: true,
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      totalCount: 0,
      offset: null // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
    }
  },

  methods: {
    async onLoad () {
      try {
        // 发请求
        const res = await getComment(this.$route.params.id, this.offset)
        console.log('shsh', res)
        // 把请求的结果追加到list
        const arr = res.data.data.results
        this.list.push(...arr)
        // 手动取消loading
        this.loading = false
        // 判断是否全部加载完成
        this.finished = !arr.length
        // 更新offset
        this.offset = res.data.data.last_id
      } catch (err) {
        console.log(err)
        this.$toast.success('获取评论失败')
        this.loading = false
      }
    },
    hBlur () {
      // 回到状态
      setTimeout(() => {
        this.commentShow = true
      })
    },
    hShowCommentArea () {
      // 状态二显示
      this.commentShow = false // 通知视图等会去更新‘
      this.$nextTick(() => {
        // 让输入框获取焦点
        this.$refs.txt.focus()
      })
    },
    // 用户点击添加
    async hAddComment () {
      if (!this.commentText) {
        return
      }
      try {
        const res = await addComment(this.$route.params.id, this.commentText)
        console.log(res)
        this.$toast.success('添加评论成功')
        // 当前评论数据
        res.data.data.new_obj.is_liking = false
        this.list.unshift(res.data.data.new_obj)
      } catch (err) {
        console.log(err)
        this.$toast.fail('添加失败')
      }
    },
    async hToggleLike (item) {
      try {
        console.log(item)
        const isLiking = item.is_liking
        const commentId = item.com_id
        if (isLiking) {
          await deleteCommentLike(commentId)
        } else {
          await addCommentLike(commentId)
        }
        this.$toast.success('操作成功')
        // 更新本地数据
        item.is_liking = !isLiking
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    }

    // onLoad () {
    //   // 异步更新数据
    //   // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    //   setTimeout(() => {
    //     for (let i = 0; i < 10; i++) {
    //       this.list.push(this.list.length + 1)
    //     }

    //     // 加载状态结束
    //     this.loading = false

    //     // 数据全部加载完成
    //     if (this.list.length >= 40) {
    //       this.finished = true
    //     }
    //   }, 1000)
    // }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}

/*美化 - 文章详情 - 底部的发布评论-样式 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
